

package linkeddataweb.web;

import java.io.DataOutputStream;
import java.io.File;
import java.io.FileOutputStream;
import java.io.FileWriter;
import java.io.IOException;
import java.io.PrintWriter;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import linkeddataweb.professor.Professor;

import com.hp.hpl.jena.query.Dataset;
import com.hp.hpl.jena.rdf.model.Model;
import com.hp.hpl.jena.rdf.model.ModelFactory;
import com.hp.hpl.jena.rdf.model.Resource;
import com.hp.hpl.jena.sparql.vocabulary.FOAF;
import com.hp.hpl.jena.tdb.TDBFactory;
import com.hp.hpl.jena.util.FileManager;
import com.hp.hpl.jena.vocabulary.DC;
import com.hp.hpl.jena.vocabulary.VCARD;

public class ProfessorServlet extends HttpServlet {

	@Override
	public void doPost(HttpServletRequest request, HttpServletResponse response)
			throws IOException, ServletException {

		Professor p = new Professor();

		p.setName(request.getParameter("name"));
		p.setCitationName(request.getParameter("cName"));
		p.setAddress(request.getParameter("address"));
		p.setEmail(request.getParameter("email"));
		p.setUniversityName(request.getParameter("uName"));
		p.setWebpage(request.getParameter("webPage"));
		p.setLattes(request.getParameter("lattes"));
		p.setImage(request.getParameter("image"));
		p.setHigherDegree(request.getParameter("hDegree"));
		p.setProfissionalArea(request.getParameter("pArea"));
		p.setProject(request.getParameter("project1"));
		p.setPastProject(request.getParameter("project2"));

		String[] event = new String[2];
		event[0] = request.getParameter("event1");
		event[1] = request.getParameter("event2");

		String[] language = new String[2];
		language[0] = request.getParameter("language1");
		language[1] = request.getParameter("language2");

		String[] paper = new String[2];
		paper[0] = request.getParameter("paper1");
		paper[1] = request.getParameter("paper2");

		String[] knows = new String[2];
		knows[0] = request.getParameter("knows1");
		knows[1] = request.getParameter("knows2");

		
		// create an empty model and dataset folder
		
		Model model = ModelFactory.createDefaultModel();
		
		try {
			if (request.getParameter("sendButton") != null) {

				// create the resource
				// and add the properties cascading style
				Resource professor = model
						.createResource(p.getWebpage())
						.addProperty(FOAF.name, p.getName())
						.addProperty(FOAF.nick, p.getCitationName())
						.addProperty(VCARD.ADR, p.getAddress())
						.addProperty(VCARD.EMAIL, p.getEmail())
						.addProperty(VCARD.ORG, p.getUniversityName())
						.addProperty(FOAF.homepage, p.getWebpage())
						.addProperty(FOAF.workInfoHomepage, p.getLattes())
						.addProperty(VCARD.PHOTO, p.getImage())
						.addProperty(FOAF.title, p.getHigherDegree())
						.addProperty(FOAF.topic_interest,
								p.getProfissionalArea())
						.addProperty(FOAF.currentProject, p.getProject())
						.addProperty(FOAF.pastProject, p.getPastProject())
						.addProperty(DC.language, language[0])
						.addProperty(DC.language, language[1])
						.addProperty(FOAF.publications, paper[0])
						.addProperty(FOAF.publications, paper[1])
						.addProperty(VCARD.Other, event[0])
						.addProperty(VCARD.Other, event[1])
						.addProperty(FOAF.knows, knows[0])
						.addProperty(FOAF.knows, knows[1]);

				
				//Stores a RDF documents in the store folder
				FileWriter f = new FileWriter("C:\\store\\"+p.getName()+".rdf");
		        model.write(f);
		    	

		        //If the user wants to download the file
				PrintWriter out = response.getWriter();
				
				response.setContentType("application/rdf+xml");
				response.setHeader("Content-Disposition",
						"attachment; filename=myRDF.rdf");
				model.write(out);
				model.close(); // closing the model
				
				//Creates the triple databases
		    	String directory = "C:\\mydataset";
				Dataset ds = TDBFactory.createDataset(directory);
				Model model2 = ds.getDefaultModel();
				
				//Read the model and save it in the mydataset folder
				FileManager.get().readModel(model2, "C:\\store\\"+p.getName()+".rdf", "RDF/XML");
				model2.close();

			}
		} catch (Exception e) {
			System.out.println(e);
		}finally{
		
		}

	}

}
